package com.kila.converter;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 对象进行转换处理的对象
 *
 * 转换器的应用
 *
 */


public class BooleanAndIntConverter extends BaseTypeHandler<Boolean> {
    /**
     * setNonNullParameter() METHOD
     *
     *
     * @param preparedStatement   preparedStatement对象
     * @param i                   对象操作的参数的位置
     * @param aBoolean            java需要转换的对象
     * @param jdbcType            jdbc的类型
     * @throws SQLException
     */


    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Boolean aBoolean, JdbcType jdbcType) throws SQLException {
              if(aBoolean){
                  //1
                  preparedStatement.setInt(i,1);
              }else{
                  //0
                  preparedStatement.setInt(i,0);
              }
    }


    /**
     *
     * @param resultSet   resultSet对象
     * @param s   列的名字
     * @return  将数据库对象转换为java对象
     * @throws SQLException
     */


    @Override
    public Boolean getNullableResult(ResultSet resultSet, String s) throws SQLException {
        int sexNum = resultSet.getInt(s);
        return sexNum ==1?true:false;

    }

    @Override
    public Boolean getNullableResult(ResultSet resultSet, int i) throws SQLException {
        int sexNum = resultSet.getInt(i);
        return sexNum ==1?true:false;
    }

    @Override
    public Boolean getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        int sexNum = callableStatement.getInt(i);
        return sexNum ==1?true:false;
    }
}
